layui.define(['element', 'api'], function(exports) {
	"use strict";

	/**
	 * Button component
	 * */
	var MOD_NAME = 'amis',
		$ = layui.jquery,
		api = layui.api,
        amis,
        amisLib,
        amisScoped
    ;

	var amis = function(opt) {
		this.option = opt;
	};


    /**
	 * Button start loading
	 * */
    amis.prototype.render = function(opt) {
		
        // if(layui.amis.amisScoped){
        //     layui.amis.amisScoped.unmount();
        // }
        

		var option = {
			elem: opt.elem,
            schema: opt.schema,
            schemaApi: opt.schemaApi,
			props: opt.props,
            extension: opt.extension,
            amisScoped: null,
            amisLib: null
		}


        option.amisLib = amisRequire('amis');
        let amisObj = amisRequire('amis/embed');

        $.getJSON(window.rootPath.replace('/js', '/data')+'icon.json',function(icons) {
            //设置图标方法
            option.amisLib.setIconVendor([
                {
                    name: 'Font Awesome 4.7',
                    prefix: 'fa fa-',
                    icons: icons
                }
            ]);
        });
		if(option.schema){
            option.schema = option.schema;
		}else if(option.schemaApi){
            api.get({
                type: "GET",
                url: option.schemaApi,
                async: false,
                success: function(data){
                    option.schema = JSON.parse(data);
                }
            })
		}
		// if(!option.extension){
        //     option.extension = {
        //         theme : 'antd'
        //     }
        // }

        option.amisScoped = amisObj.embed(option.elem, option.schema, option.props, option.extension);

        this.amis = amisObj;
        this.amisLib = option.amisLib
        this.amisScoped = option.amisScoped
        return new amis(option);
	}


    exports(MOD_NAME, new amis());
});
